package cn.edu.sdjzu.xg.kcsj.service;

import cn.edu.sdjzu.xg.kcsj.dao.SADao;
import cn.edu.sdjzu.xg.kcsj.domain.Faculty;
import cn.edu.sdjzu.xg.kcsj.domain.SA;
import cn.edu.sdjzu.xg.kcsj.exception.BysjException;
import cn.edu.sdjzu.xg.kcsj.security.User;
import cn.edu.sdjzu.xg.kcsj.security.UserService;
import cn.edu.sdjzu.xg.kcsj.util.JdbcHelper;
import cn.edu.sdjzu.xg.kcsj.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.TreeSet;

public class SAService {
    private static SADao sADao = SADao.getInstance();
    private static SAService sAService = new SAService();

    private SAService() {
    }

    public static SAService getInstance() {
        return sAService;
    }

    //分页查询
    public Collection<SA> findAll(List conditionList, Pagination pagination) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Collection<SA> sas = new TreeSet<SA>();
        try {
            sas = sADao.findAll(conditionList,pagination,connection);
            //提交事务
            connection.commit();
        }catch (SQLException | BysjException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return sas;
    }

    // 一个学生报名的所有活动
    public Collection<SA> getAllActivitiesOfStudent(User student) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Collection<SA> sas = new TreeSet<>();
        try {
             sas= sADao.getAllActivitiesOfStudent(student);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return sas;
    }
//按照id查询
    public SA find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        SA sa = null;
        try {
            sa = sADao.find(id,connection);
            //提交事务
            connection.commit();
        }catch (SQLException | BysjException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return sa;
    }

    //更新
    public boolean update(SA sa) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean updated =false;
        try {
            updated = sADao.update(sa,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return updated;
    }

    public boolean add(SA sa) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean added =false;
        try {
            //增加
            added = sADao.add(sa,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();

        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added ;
    }
    //删除
    public boolean delete(Integer id) throws SQLException,BysjException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean deleted = false;
        try {
            deleted = sADao.delete(id, connection);
                //提交事务
                connection.commit();
        } catch (SQLException e) {
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted;
    }


}


